package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.CustomException;
import com.yuesystem.entity.AcademyItem;
import com.yuesystem.entity.FixedItem;
import com.yuesystem.mapper.AcademyItemMapper;
import com.yuesystem.service.AcademyItemService;
import com.yuesystem.service.FixedItemService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 书院条目接口实现类.
 */
@Service
@Slf4j
public class AcademyItemServiceImpl extends
    ServiceImpl<AcademyItemMapper, AcademyItem> implements AcademyItemService {

  @Autowired
  private FixedItemService fixedItemService;

  /**
   * 根据书院id获得该书院可变条目信息.
   *
   * @param id 书院id
   * @return 书院可变条目列表
   */
  @Override
  public List<AcademyItem> getItems(Long id) {
    // 展示未删除的修改信息
    LambdaQueryWrapper<AcademyItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(AcademyItem::getAcademyId, id)
        .eq(AcademyItem::getIsDeleted, 0)
        .ne(AcademyItem::getRelation, "位于")
        .ne(AcademyItem::getRelation, "修建时间");

    List<AcademyItem> list = this.list(queryWrapper);
    return list;
  }

  /**
   * 根据书院id获得固定条目信息.
   *
   * @param id 书院id
   * @return 书院固定条目列表
   */
  @Override
  public List<FixedItem> getFixed(Long id) {
    LambdaQueryWrapper<FixedItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(FixedItem::getAcademyId, id);
    List<FixedItem> list = fixedItemService.list(queryWrapper);
    return list;
  }


  /**
   * 根据书院条目id获得条目信息.
   *
   * @param id 书院条目id
   * @return 书院条目类
   */
  @Override
  public AcademyItem getItemById(Long id) {

    LambdaQueryWrapper<AcademyItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(AcademyItem::getIsDeleted, 0);
    queryWrapper.eq(AcademyItem::getId, id);
    AcademyItem item = this.getOne(queryWrapper);
    if (item == null) {
      throw new CustomException("该条目不存在，请刷新后重试！");
    }
    return item;
  }


}
